//
//  VictoryScreen.m
//  TowerDefence
//
//  Created by k3 on 6/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VictoryScreen.h"
#import "iCoreGUIController.h"

@implementation VictoryScreen

-(id)initVictorySreenWith:(iCoreGUIController *)_mainGUI withBonus:(int)_bonus{
    self = [super initGameLayerWith:_mainGUI];
    bonus=_bonus;
    [NSThread detachNewThreadSelector:@selector(loadScreen) toTarget:self withObject:nil];
    return self;
}

-(void)loadScreen {    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    EAGLContext *k_context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:[[[mainGUIController.mainDirector openGLView] context] sharegroup]] autorelease];
    [EAGLContext setCurrentContext:k_context];
    
    [mainGUIController loadGraphicFromPlistFileToFrameCache:PLIST_SHINE_EFFECT];
    [mainGUIController loadGraphicFromPlistFileToFrameCache:PLIST_RESOURCE_STAR_EFFECT];
    
    CGSize size = [[CCDirector sharedDirector] winSize];
    blankImg = [[CCSprite alloc]initWithFile:BLANK_IMAGE];
    [blankImg setPosition:ccp(0, 0)];
    [blankImg setAnchorPoint:ccp(0, 0)];
    [blankImg setColor:ccc3(255, 255, 255)];
    [blankImg setOpacity:0];
    [self addChild:blankImg z:0];
    
    victoryLogo = [[CCSprite alloc]initWithFile:IMAGE_MEDIUM_VICTORY];
    [victoryLogo setPosition:ccp(size.width/2, size.height/2+30)];
    [victoryLogo setAnchorPoint:ccp(0.5, 0.5)];
    [self addChild:victoryLogo z:1];
    [victoryLogo runAction:[CCRepeatForever actionWithAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.5f scale:0.9],[CCScaleTo actionWithDuration:0.7f scale:1.0f], nil]]];

    CCAnimation *animation = [mainGUIController loadAnimationFromCacheByName:@"star_effect" withCount:24 delay:0.05];
    
    listEffectNode = [[NSMutableArray alloc] init];
    for(int i = 1; i<=50 ; i++){
        CCSprite *singleEffect = [[CCSprite alloc]init];
        [singleEffect runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]]];
        [singleEffect setPosition:ccp(arc4random()%480, arc4random()%320)];
        [listEffectNode addObject:singleEffect];
        [self addChild:singleEffect];
    }
    
    playChainMenuItem = [[CCSprite alloc]initWithFile:IMAGE_MAIN_MENU_ITEM_CHAIN];
    playChainMenuItem.scale = 0.8;
    //playChainMenuItem.visible = YES;
    playChainMenuItem.anchorPoint = ccp(0.5f, 1.0f);
    playChainMenuItem.position = ccp(size.width/2, victoryLogo.position.y + playChainMenuItem.contentSize.height/2-120);
    [self addChild:playChainMenuItem z:0];
    
    for(int i=0;i<bonus;i++){
        CCSprite *starIcon = [CCSprite spriteWithFile:IMAGE_PLAYER_INFO_STAR];
        [self addChild:starIcon z:2];
        starIcon.scale=0;
        starIcon.position=ccp(200+i*40,100);
        [starIcon runAction:[CCScaleTo actionWithDuration:1.0 scale:1]];
        CCSprite *starEffect = [CCSprite node];
        starEffect.position = ccp(starIcon.contentSize.width/2, 10);
        [starIcon addChild:starEffect];
        [starEffect runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:YES]]];
    }
    
    
    CCLabelTTF *continueLbl = [CCLabelTTF labelWithString:@"Continue" fontName:FONT_MARKER_FELT fontSize:28];
    continueLbl.color = ccc3(255, 255, 0);

    CCMenuItemLabel *continueMenuItemLabel = [CCMenuItemLabel itemWithLabel:continueLbl target:self selector:@selector(BackToMapScreen)];
    continueMenuItemLabel.position = ccp(playChainMenuItem.contentSize.width/2-70, playChainMenuItem.contentSize.height-140);
    continueMenuItemLabel.anchorPoint = ccp(0.5f,0.5f);

    CCMenu *continueMenu = [CCMenu menuWithItems:continueMenuItemLabel, nil];
    continueMenu.position = ccp(playChainMenuItem.contentSize.width/2, playChainMenuItem.contentSize.height -50);
    [playChainMenuItem addChild:continueMenu z:5];
    
    [mainGUIController playSoundPlayer:SOUND_PLAYER_STANDARD_BACKGROUND fileName:AUDIO_VICTORY withLoop:NO];
    [mainGUIController setVolumeForPlayer:SOUND_PLAYER_STANDARD_BACKGROUND value:0.25f];
    
    [pool release];
}

-(void)BackToMapScreen {
    [mainGUIController PlayMapScreenWithCurrentScreen:self];
}

-(void)dealloc {
    NSLog(@"victory dealloc");
    [blankImg stopAllActions];
    [blankImg removeFromParentAndCleanup:YES];
    [blankImg release];
    
    [victoryLogo stopAllActions];
    [victoryLogo removeFromParentAndCleanup:YES];
    [victoryLogo release];
    
    [playChainMenuItem stopAllActions];
    [playChainMenuItem removeFromParentAndCleanup:YES];
    [playChainMenuItem release];
    
    for(int i =0 ; i< listEffectNode.count; i++) {
        CCSprite *temp =[[CCSprite alloc]initWithTexture:[listEffectNode objectAtIndex:i]];
        [temp stopAllActions];
        [temp removeFromParentAndCleanup:YES];
        [temp release];
    }
    
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    
    [super dealloc];
}

@end
